package edu.luc.cs.trull.demo.helloworld;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;

import edu.luc.cs.trull.ActionComponent;
import edu.luc.cs.trull.AwaitOne;
import edu.luc.cs.trull.Composite;
import edu.luc.cs.trull.DefaultTopLevelComponent;
import edu.luc.cs.trull.Loop;
import edu.luc.cs.trull.swing.ActionEventAdapter;
import edu.luc.cs.trull.swing.SwingScheduler;

/**
 * The obligatory HelloWorld demo.
 */
public class HelloWorld implements EventLabels {
	
	private static int buttonCount = 0;

	private static int eventCount = 0;
	
  public static void main(String[] args) throws Exception {
    // presentation component
  	final JButton button = new JButton(HELLO);

  	final ActionEventAdapter adapter = new ActionEventAdapter();
    button.addActionListener(adapter);
  	
    // behavior
    final Composite top = new Composite();
    top.addComponent(adapter);
    top.addComponent(new Loop(new AwaitOne(HELLO, new ActionComponent(new PropertyChangeListener() {
    	public void propertyChange(PropertyChangeEvent event) {
    		++eventCount;
    		if (buttonCount % 100 == 0)
    			System.out.println(buttonCount + " " + eventCount);
    	}
    }))));
    
    
  	button.addActionListener(new ActionListener() {
  		public void actionPerformed(ActionEvent event) {
  			++buttonCount;
  		}
  	});
    
    new DefaultTopLevelComponent(top, new SwingScheduler()).start();
    
    // rest of presentation
    JFrame frame = new JFrame("Hello World Demo");
    frame.getContentPane().add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.show();
  }
}
